package com.maochd.cloud.common.core.utils.request;

import com.alibaba.fastjson.JSON;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {

    /**
     * 获取请求
     *
     * @return request
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }

    /**
     * 获取请求头
     *
     * @param header 请求头
     * @param clazz  类型
     * @return object
     */
    public static <T> T header(String header, Class<T> clazz) {
        return JSON.parseObject(getRequest().getHeader(header), clazz);
    }
}
